<template>
  <div>
    <top></top>
    <mall-header @onSearch="onSearch"></mall-header>
    <infoPopover :supplier="detail"></infoPopover>
    <choose-type default-type-id="allall" @change="typeChange" :isShop="false"></choose-type>
    <div class="filter-wrapper">
      <filter-item :default-params="deliveryParams" :data="dataDictLists['DELIVERY_FORM']" title="交付形态"
                   @ok="handleFilter($event,'deliveryParams')"></filter-item>

      <filter-item :default-params="industryParams" :data="displayApplicationIndustry" title="应用行业"
                   @ok="handleFilter($event,'industryParams')"></filter-item>

      <filter-item :default-params="sceneParams" :data="dataDictLists['APPLICATION_SCENARIO']" title="应用场景"
                   @ok="handleFilter($event,'sceneParams')"></filter-item>

      <filter-item :default-params="supplierParams" display-key="supplierName" value-key="supplierId"
                   :data="supplierList" title="数据商"
                   @ok="handleFilter($event,'supplierParams')"></filter-item>
    </div>
    <div class="sort-wrapper">
      <div class="sort-left">
        排序方式：
      </div>
      <div class="sort-right" v-for="item in sortList" @click="changeSort(item.value)" :key="item.value"
           :class="{'active':params.sortType === item.value}">
        {{ item.name }}
      </div>
      <div style="flex: 1;text-align: right;padding-right: 20px;">热门推荐：
        <a-switch v-model="params.isHot" @change="loadData"/>
      </div>
    </div>
    <div class="goods-wrapper">
      <a-empty v-if="goodsList.length===0" style="width: 100%;margin: 20px 0;"></a-empty>
      <template v-else>
        <div class="goods-item" v-for="item in goodsList" :key="item.goodsId">
          <div>
            <div class="goods-item-content">
              <img :src="item.thumbnail" alt=""/>
              <div class="goods-item-content-right">
                <div class="issjrz" v-if="item.authOrganFullName == '深圳数据交易所' && item.isShowFlag">
                  <a-tooltip :title="'经' + item.authOrganFullName + '合规评估'">
                    <img src="../../assets/z.png"/>
                  </a-tooltip>
                </div>
                <div class="goods-item-name" :style="{color: item.isPublic || '#aaa'}" @click="toDetail(item.goodsId,item.authOrganFullName)">
                  {{ item.goodsName }}
                </div>
                <div style="display: flex;margin: 6px 0">
                  <a-tag color="orange" v-if="item.chargeType === 2 "> 免费 </a-tag>
                  <a-tag color="orange" v-else-if="item.freeNums > 0 && item.freeDimension == 1"> 免费{{item.freeNums}}天 </a-tag>
                  <a-tag color="orange" v-else-if="item.freeNums > 0 && item.freeDimension == 2"> 免费{{item.freeNums}}次 </a-tag>
                  <a-tag color="blue" v-if="item.purchasePermission === 2"> 企业 </a-tag>
                </div>
                <div class="goods-item-supplier" v-if="item.supplierName">
                  {{ item.supplierName }}
                </div>
                <div class="goods-item-content-bottom">
                  <div class="itemprice" :class="{itemflex: item.chargeType == 3}" v-if="item.activityDesc">
                      <span style="line-height: 20px;">{{ item.activityDesc }}</span>
                    <!-- <span class="unit">{{ item.chargeType === 1 ? item.unit : '' }}</span> -->
                    <div class="through">
                      {{ item.chargeType == 3 ? '面议' : ('¥' + (item.chargeType == 1 ? item.price : 0)) }}
                      {{ (item.chargeType === 1 ? item.unit : '') }}
                    </div>
                  </div>
                  <div class="goods-item-price" v-else>
                    <div style="line-height: 20px;"> {{item.chargeType == 3 ? '面议' : ('¥' + (item.chargeType == 1 ? item.price : 0))}}</div>
                    <span class="goods-item-unit">{{ item.chargeType === 1 ? item.unit : '' }}</span>
                  </div>
                  <rate-star :rate="item.goodsRate" show-bg></rate-star>
                </div>
                <!-- <div style="color: #aaa; font-size: 10px;" v-if="item.authOrganFullName && item.authOrganFullName != '深圳数据交易所'">{{item.authOrganFullName}}认证</div> -->
              </div>
            </div>
            <div class="goods-item-desc more-t">
              {{ item.goodsDesc }}
            </div>
          </div>
          <div class="goods-item-bottom">
            <div class="goods-item-type">
              <svg-icon icon-class="data_type" size="14" style="margin-right: 3px;"></svg-icon>
              应用行业：
              <span class="goods-item-type-content">{{ item.applicationIndustry }}</span>
            </div>
            <div class="goods-item-type">
              <svg-icon icon-class="data_scene" size="14" style="margin-right: 3px;"></svg-icon>
              应用场景：
              <span class="goods-item-type-content">{{ item.applicationScenario }}</span>
            </div>
          </div>
        </div>
      </template>
    </div>
    <div class="pagination">
      <paging
        :total="paging.total"
        :current="paging.page"
        :show-size-changer="false"
        :defaultPageSize="paging.limit"
        @pageChange="handlePageChange"
        ref="paging"
      />
    </div>
    <mall-footer></mall-footer>
  </div>
</template>

<script>
import Top from '@/components/base/Top'
import MallHeader from '@/components/base/MallHeader'
import Type from '@/components/Type'
import Paging from '@/components/base/Paging'
import MallFooter from '@/components/base/MallFooter'
import {dictService} from '@/api/common'
import {pageMixin} from '@/mixins'
import {searchGoodsPage} from '../../api/goods'
import ChooseType from '../../components/ChooseType'
import RateStar from '../../components/RateStar'
import {qryBrandSupplierList} from '../../api/portal'
import FilterItem from './components/FilterItem'
import infoPopover from '../shop/components/infoPopover'
import { getShop } from '@/api/shop'

export default {
  name: 'goodsList',
  mixins: [pageMixin],
  components: {
    FilterItem,
    RateStar,
    ChooseType,
    Top,
    MallHeader,
    Type,
    Paging,
    MallFooter,
    infoPopover
  },
  data () {
    return {
      dataDictMap: {
        DELIVERY_FORM: {},
        GOODS_CHARGE_TYPE: {}
      },
      dataDictLists: [],
      params: {
        'applicationIndustry': '',
        'applicationScenario': '',
        'deliveryForm': '',
        'goodsIdList': [],
        'goodsName': '',
        'goodsTypeId': '',
        'goodsTypeIdList': [],
        'isHot': false,
        'order': '',
        'orderBy': '',
        'payModel': '',
        'sortType': 0,
        'status': '',
        'supplierId': '',
        'supplierIdList': [],
        'supplierName': '',
      },
      defaultTypeId: '',
      supid: '',
      detail:{},
      sortList: [
        {name: '综合排序', value: 0},
        {name: '最新发布', value: 10},
        {name: '最优评价', value: 20},
        {name: '最多使用', value: 30},
      ],
      goodsList: [],
      supplierList: [],
      supplierParams: [],
      sceneParams: [],
      industryParams: [],
      deliveryParams: [],
    }
  },
  computed: {
    displayApplicationIndustry () {
      return this.dataDictLists['APPLICATION_INDUSTRY'] ? this.dataDictLists['APPLICATION_INDUSTRY'].sort((a, b) => a.name.length - b.name.length) : []
    }
  },
  created () {
    dictService(this, ['DELIVERY_FORM', 'APPLICATION_INDUSTRY', 'APPLICATION_SCENARIO'])
    this.loadSupplier()
    this.supid = this.$route.query.supid || ''
    this.defaultTypeId = this.$route.query.type || ''
    this.params.goodsTypeId = this.$route.query.type || ''
    this.params.goodsName = this.$route.query.title || ''
    const deliveryForm = this.$route.query.deliveryForm || ''
    if (deliveryForm) {
      this.deliveryParams.push(deliveryForm)
    }
    const supplierId = this.$route.query.supplier || ''
    if (supplierId) {
      this.supplierParams.push(supplierId)
    }
    this.params.isHot = this.$route.query.hot
    this.paging.limit = 15
    this.loadData()
    this.getShop(this.supid)
  },
  methods: {
    async getShop(supplierId){
      let res = await getShop({supplierId});
      this.detail = res.data
    },
    loadData () {
      const params = {
        ...this.params,
        supplierIdList: this.supplierParams,
        applicationScenario: this.sceneParams.join(','),
        applicationIndustry: this.industryParams.join(','),
        deliveryForm: this.deliveryParams.join(','),
        supplierId: this.supid,
        page: this.paging.page,
        limit: this.paging.limit
      }
      searchGoodsPage(params).then(res => {
        if (res.code === 0) {
          this.goodsList = res.data.records
          this.paging.total = Number(res.data.total) || 0
        }
      })
    },
    loadSupplier () {
      qryBrandSupplierList({page: 1, limit: 999}).then(res => {
        if (res.code === 0) {
          this.supplierList = res.data.records
        }
      })
    },
    onSearch (e) {
      this.params.goodsName = e || ''
      this.loadData()
    },
    handlePageChange (page, pageSize) {
      this.paging.page = page
      this.paging.limit = pageSize
      this.loadData()
    },
    toDetail (id, authOrganFullName) {
      this.$router.push({
        path: '/goodsDetail',
        query: {
          id: id,
          authorganFulIName: authOrganFullName
        }
      })
    },
    /**
     * 商品类型
     * @param e
     */
    typeChange (e) {
      let { id, pid } = e
      let gid = pid || (id == 'allall' ? '' : id)
      this.defaultTypeId = id
      this.params.goodsTypeId = pid || gid
      this.supplierParams = []
      this.sceneParams = []
      this.industryParams = []
      this.deliveryParams = []
      this.loadData()
    },
    /**
     * 排序
     * @param code
     */
    changeSort (code) {
      this.params.sortType = code
      this.loadData()
    },
    /**
     * 查询条件
     * @param e
     * @param key
     */
    handleFilter (e, key) {
      this._data[key] = e
      this.loadData()
    }
  }
}
</script>

<style scoped lang="scss">
$width: 1200px;
$mainColor: #0078FE;

/deep/ .ant-tag {
  background-color: #fff !important;
}

.filter-wrapper {
  width: $width;
  margin: 20px auto;
  background: #FFFFFF;
  box-shadow: 0 0 10px 0 rgba(0, 120, 254, 0.1);
  border-radius: 10px;
}

.sort-wrapper {
  width: $width;
  margin: 0 auto;
  padding-left: 20px;
  height: 40px;
  background: #FAFAFA;
  border-radius: 6px;
  border: 1px solid #E2E2E2;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC, sans-serif;
  font-weight: 400;
  display: flex;
  align-items: center;

  .sort-left {
    color: #646464;
    width: 100px;
  }

  .sort-right {
    color: #323232;
    margin-right: 40px;
    cursor: pointer;

    &:hover {
      color: $mainColor;
    }

    &.active {
      color: $mainColor;
    }
  }
}

.goods-wrapper {
  margin: 0 auto;
  width: $width;
  display: flex;
  flex-wrap: wrap;

  .goods-item {
    height: 240px;
    width: calc((100% - 40px) / 3);
    margin-left: 20px;
    box-shadow: 0 0 10px 0 rgba(#0078FE, 0.1);
    background-color: white;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    margin-top: 20px;
    flex-direction: column;
    justify-content: space-between;

    &:nth-child(3n+1) {
      margin-left: 0;
    }

    .goods-item-content {
      display: flex;
      // align-items: center;
      padding-bottom: 20px;
      margin-bottom: 20px;
      // height: 110px;
      border-bottom: 1px dotted #E2E2E2;

      img {
        width: 80px;
        height: 80px;
      }

      .goods-item-content-right {
        margin-left: 16px;
        width: calc(100% - 99px);
        position: relative;
        // flex: 1;
        // width: 0;
        // height: 100px;
        // display: flex;
        // flex-direction: column;
        // justify-content: space-between;
        .issjrz{
          position: absolute;
          top: -6px;
          right: -6px;
          img{
            height: 24px;
            width: 24px;
          }
        }
        .goods-item-name {
          font-size: 14px;
          cursor: pointer;
          line-height: 14px;
          font-family: PingFangSC-Medium, PingFang SC, sans-serif;
          font-weight: 500;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          color: #323232;
          width: 90%;
          &:hover {
            color: $mainColor;
          }
        }

        .goods-item-supplier {
          font-size: 12px;
          line-height: 12px;
          font-family: PingFangSC-Regular, PingFang SC, sans-serif;
          color: $mainColor;
          padding: 5px 10px;
          border-radius: 2px;
          border: 1px solid #CCE4FE;
          width: fit-content;
          max-width: 100%;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          margin-bottom: 6px;
        }

        .goods-item-content-bottom {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .through{
            text-decoration: line-through;
            color: #999;
            font-size: 12px;
            margin-left: 4px;
          }
          .itemflex{
            white-space:nowrap;
            display: flex;
            align-items: self-end;
          }
          .itemprice{
            font-size: 20px;
            color: #E03437;
            font-weight: 500;
            .unit{
              font-size: 12px;
              font-weight: 400;
              color: #909090;
              margin-left: 6px;
            }
          }
          .goods-item-price {
            font-size: 20px;
            font-family: PingFangSC-Medium, PingFang SC, sans-serif;
            font-weight: 500;
            color: #E03437;
            white-space:nowrap;
            display: flex;
            align-items: flex-end;

            .goods-item-unit {
              font-size: 12px;
              font-family: PingFangSC-Regular, PingFang SC, sans-serif;
              font-weight: 400;
              color: #909090;
              margin-left: 6px;
            }
          }
        }
      }
    }

    .goods-item-desc {
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC, sans-serif;
      font-weight: 400;
      color: #646464;
      line-height: 15px;
    }

    .goods-item-bottom {
      display: flex;
      align-items: flex-start;

      .goods-item-type {
        flex: 1;
        width: 0;
        display: flex;
        align-items: flex-start;
        font-size: 12px;
        font-family: PingFangSC-Regular, PingFang SC, sans-serif;
        font-weight: 400;
        color: #646464;
        line-height: 14px;

        &:last-child {
          margin-left: 5px;
        }

        .goods-item-type-content {
          color: #323232;
          flex: 1;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }
}

.pagination {
  width: $width;
  margin: 20px auto;
  padding-bottom: 20px;
}
</style>
